package com.mhy.base.fileprovider;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.StrictMode;
import android.provider.MediaStore;

import androidx.annotation.IntDef;
import androidx.core.content.FileProvider;

import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created By Mahongyin
 * Date    2021/3/17 19:32
 * 统一适配 N FileProvider
 */
public class FileProviderUtil {


    /**
     * 仅获取 Uri
     */
    public static Uri getUriForFile(Context context, File file) {
        Uri fileUri = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            fileUri = getUriForFile24(context, file);
        } else {
            fileUri = Uri.fromFile(file);
        }
        return fileUri;
    }
    /**
     * android N
     */
    private static Uri getUriForFile24(Context context, File file) {
        String authority = context.getPackageName() + ".fileprovider";
        Uri fileUri = FileProvider.getUriForFile(context, authority, file);
        return fileUri;
    }
    /**
     * intent.setData()
     * FileProviderUtil.getUriIntentData(mContext,new File(photoSavePath, photoSaveName),openCameraIntent,Intent.FLAG_GRANT_READ_URI_PERMISSION);
     */
    public static void getUriIntentData(Context context, File file, Intent intent, @FlagType int flag) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                //照片、视频、音频这类媒体文件。使用MediaStore 访问，访问其他应用的媒体文件时需要READ_EXTERNAL_STORAGE权限。
                intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, getUriForFile24(context, file));
            } else {
                intent.setData(getUriForFile24(context, file));
            }
            intent.addFlags(flag);
        } else {
            intent.setData(Uri.fromFile(file));
        }
    }

    /**
     * intent.setDataAndType()
     */
    public static void getUriIntentDataAndType(Context context, File file, Intent intent, /*String type,*/ boolean writeAble) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            Uri uri = getUriForFile24(context, file);
            intent.setDataAndType(uri, context.getContentResolver().getType(uri));//getContentResolver().getType(uri)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            if (writeAble) {
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        } else {
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, context.getContentResolver().getType(uri));
        }
    }

    @IntDef({Intent.FLAG_GRANT_READ_URI_PERMISSION, Intent.FLAG_GRANT_WRITE_URI_PERMISSION})
    @Retention(RetentionPolicy.SOURCE)
    public @interface FlagType {}


//    绕过版本限制，删除Uri的检测，这样就可以绕过7.0的文件共享限制
    /**
     * 需要在Application中执行
     */
    private void detectFileUriExposure() {
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            builder.detectFileUriExposure();
        }
      //  builder.detectAll();

    }

}
